#!/bin/bash

if [ -z $1 ]
then
	echo "Missing commandline argument!"
	echo "Usage: ./$0 <p4-program-name>"
	echo "Example: ./$0 l2switch"
	exit 1
fi

DEFAULT_PROG=$1
T4P4SDIR="/root/t4p4s"
P4RTDIR="${T4P4SDIR}/examples/p4rt_files"


if [ ! -f "${T4P4SDIR}/examples/${DEFAULT_PROG}.p4" ]
then
	echo "P4 source file does not exist."
	echo "Missing file: ${T4P4SDIR}/examples/${DEFAULT_PROG}.p4"
	exit 1
fi

echo "Generating P4Runtime files..."

if [ ! -d "$P4RTDIR" ]
then
	echo "Creating the directory for P4Runtime files: $P4RTDIR"
	mkdir $P4RTDIR
fi

pushd ${P4RTDIR}
p4c-bm2-ss --p4runtime-files "${DEFAULT_PROG}.p4runtime.txt" --toJSON "${DEFAULT_PROG}.json" "${T4P4SDIR}/examples/${DEFAULT_PROG}.p4"
popd

echo "Launching P4Runtime-shell..."
python3.9 -m p4runtime_sh --grpc-addr localhost:50051 --device-id 1 --election-id 0,1 --config "${P4RTDIR}/${DEFAULT_PROG}.p4runtime.txt,${P4RTDIR}/${DEFAULT_PROG}.json"

